package com.school.chapter03.sequenceTable;

import java.util.Arrays;

@SuppressWarnings({"all"})
public class SepList {
    private int[] r;
    private int curlen;

    public SepList(int maxSize) {
        this.r = new int[maxSize];
        this.curlen = 0;
    }

    //带哨兵的顺序查找
    public int seqSearchWithGuard(int key) {
        int i = r.length - 1;
        r[0] = key;
        while (r[i] == key) {
            i--;
        }
        if (i > 0)
            return i;
        else
            return -1;

    }

    //二分查找算法
    public int BinarySearch(int key) {
        if (r.length > 0) {
            int low = 0;
            int high = r.length - 1;

            while (low <= high) {
                int mid = (low + high) / 2;
                if (r[mid] == key)
                    return mid;
                else if (r[mid] > key)
                    high = mid - 1;
                else low = mid + 1;

            }
        }
        return -1;
    }

    //分块查找


    @Override
    public String toString() {
        return "SepList{" +
                "r=" + Arrays.toString(r) +
                ", curlen=" + curlen +
                '}';
    }

    public static void main(String[] args) {
        SepList sepList = new SepList(6);


        for (int i = 0; i < 6; i++) {
            sepList.r[i] = i;
        }
        System.out.println("数组：" + Arrays.toString(sepList.r));
        System.out.println("用顺序查找查找0的位置" + sepList.seqSearchWithGuard(1));
        System.out.println("用二分查找查找5的位置" + sepList.BinarySearch(5));


    }
}

// class IndexItem{
//    public int index;
//    public int start;
//    public int length;
//
//    public IndexItem(int index, int start, int length){
//        this.index = index;
//        this.start = start;
//        this.length = length;
//    }
//
//     public int getIndex() {
//         return index;
//     }
//
//     public void setIndex(int index) {
//         this.index = index;
//     }
//
//     public int getStart() {
//         return start;
//     }
//
//     public void setStart(int start) {
//         this.start = start;
//     }
//
//     public int getLength() {
//         return length;
//     }
//
//     public void setLength(int length) {
//         this.length = length;
//     }
//
//
// }
